package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.RepairDao;
import com.gable.exhibition.dao.UpkeepDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.SmsView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工程师维修，保养响应短信
 * Created by mj on 2018/3/30.
 */
public class EngineerResponseSmsThread implements Runnable {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private Long resourcesId;

    private Integer resourcesType;      //数据类型 1:报修，2：保养

    private RepairDao repairDao;

    private UpkeepDao upkeepDao;

    private String repairByDoctorUrl;

    private String upkeepByDoctorUrl;

    public EngineerResponseSmsThread(Long resourcesId, Integer resourcesType,RepairDao repairDao,
                                     UpkeepDao upkeepDao,String repairByDoctorUrl,String upkeepByDoctorUrl){
        this.resourcesId = resourcesId;
        this.resourcesType = resourcesType;
        this.repairDao = repairDao;
        this.upkeepDao = upkeepDao;
        this.repairByDoctorUrl = repairByDoctorUrl;
        this.upkeepByDoctorUrl = upkeepByDoctorUrl;
    }
    @Override
    public void run() {
        try {
            SmsView smsView = null;
            String url = "";
            if(resourcesType == 1){
                smsView = repairDao.repairForSms(resourcesId);
                url = repairByDoctorUrl;
            }else if(resourcesType == 2){
                smsView = upkeepDao.upkeepForSms(resourcesId);
                url = upkeepByDoctorUrl;
            }

            String department = smsView.getDepartment();
            String deviceName = smsView.getDeviceName();
            String sn = smsView.getSn();
            String realName = smsView.getRealName();
            String engineerExpectedTime = smsView.getEngineerExpectedTime();

            List<String> hospitalMobile = smsView.getHospitalMobile();
            if(hospitalMobile != null && hospitalMobile.size() > 0){
                try {
                    StringBuilder sb = new StringBuilder();
                    for(String phone : hospitalMobile){
                        if(StringUtil.isEmpty(phone))
                            continue;
                        sb.append(phone).append(",");
                    }
                    String mobile = sb.substring(0, sb.length() - 1);
                    String message = department+"%&"+deviceName+"-"+sn+"%&"+realName+"%&"+engineerExpectedTime;
                    Map<String,String> serverMap = new HashMap<String,String>();
                    serverMap.put("mobile",mobile);
                    serverMap.put("message",message);
                    String jsonResult = HttpHelper.postRequestResponseBodyAsString(url,serverMap);
                    logger.info("工程师预计到场给医院的短信，设备名称："+deviceName+",短信发送结果:"+jsonResult);
                } catch (Exception e) {
                    logger.info("工程师预计到场给医院的短信，设备名称："+deviceName+",短信发送失败:"+e);
                }
            }
        } catch (Exception e) {
            logger.info("工程师预计到场给医院的短信异常:"+e);
        }
    }
}
